
module.exports = () => {
  return async (ctx, next) => {
    const {
      app: { redis },
      socket,
      logger,
    } = ctx;
    console.log("ws-io, 中间件执行 socket id：" + socket.id);
   // 订阅频道并接收消息
   redis.subscribe("test", (_channel, message) => {
    logger.info(`redis-pub订阅信息：${_channel}: ${message}`);
  });
  redis.subscribe("test", (_channel, message) => {
    logger.info(`redis-pub订阅信息：${_channel}: ${message}`);
  });
    redis.on("message", (_channel, message) => {
      const msg = socket.id + "：" + message + new Date().getTime();
      console.log("收到redis-pub推送的消息：" + msg);
      socket.emit(_channel, msg);
    });
    // 监听connection事件
    socket.on("disconnect", () => {
      logger.info("断开链接");
    });
    await next();
  };
};

// redis> publish test "Hello Wonky!"
